﻿using SHKF.B2C.Mannage.AdminUI.Model;
using SHKF.B2C.MeiTuanPeiSong.Model.Dto.Order;
using SHKF.B2C.Model.Dto.Center;
using SHKF.B2C.Model.Entity.Center;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WQDSkin.Forms.Component;
using WQDSkin.Forms.Controls;

namespace SHKF.B2C.Mannage.AdminUI.View.Mtps.Order
{
    /// <summary>
    /// frmOrder.xaml 的交互逻辑
    /// </summary>
    public partial class frmOrder : BasePage
    {
        public frmOrder()
        {
            InitializeComponent();
        }
         
        private void BasePage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadCompany();
        }
         
        private void btnSearchCompany_Click(object sender, RoutedEventArgs e)
        {
            LoadCompany();
        }

        private void btnSearchOrder_Click(object sender, RoutedEventArgs e)
        {
            LoadOrder();
        }

        private void tvCompany_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            LoadShop();
        }

        private async void LoadCompany()
        {
            string CompanyName = this.txtCompany.Text;
            CompanyListDto dto = await GL.HttpClientManage.QueryCompanyList(CompanyName);

            TreeViewItem tviRoot = (TreeViewItem)this.tvCompany.Items[0];
            tviRoot.ItemsSource = null;

            if (dto.error == null)
            { 
                foreach (CompanyEntity company in dto.data)
                {
                    WQDTreeViewItem item = new WQDTreeViewItem();
                    item.DataContext = company;
                    item.Header = company.CompanyName;
                    tviRoot.Items.Add(item);
                }
 
                tviRoot.IsExpanded = true;
            }
            else
            {
                MessageBox.Show(dto.error.msg);
            }
        }

        private async void LoadShop()
        {
           if( this.tvCompany.SelectedItem != null )
            { 
                WQDTreeViewItem selectedNode = (WQDTreeViewItem)this.tvCompany.SelectedItem; 
                if(this.tvCompany.SelectData is CompanyEntity)
                {
                    CompanyEntity companyEntity = (CompanyEntity)this.tvCompany.SelectData;
                    long companySN = companyEntity.SN;
                    if (companySN > 0)
                    {
                        ShopListDto dto = await GL.HttpClientManage.ShopQuery((int)companySN, "");
                        if (dto.error == null)
                        {
                            selectedNode.Items.Clear();
                            foreach (ShopEntity shopData in dto.data)
                            {
                                WQDTreeViewItem item = new WQDTreeViewItem();
                                item.DataContext = shopData;
                                item.Header = shopData.ShopName;
                                selectedNode.Items.Add(item);
                            }
                        }
                        else
                        {
                            MessageBox.Show(dto.error.msg);
                        }
                    }
                } 
            }
          
        }

        private async void LoadOrder()
        {
            if (this.tvCompany.SelectedItem != null)
            {
                WQDTreeViewItem selectedNode = (WQDTreeViewItem)this.tvCompany.SelectedItem;
                if (this.tvCompany.SelectData is ShopEntity)
                {
                    ShopEntity shop = (ShopEntity)this.tvCompany.SelectData;
                    string shopID = shop.ShopSN.ToString();
                    string orderID = "";
                    int status = 0;
                    DateTime startDate = DateTime.Now;
                    DateTime endDate = DateTime.Now.AddDays(7);
                    OrderListDto dto = await GL.HttpClientMtps.QueryOrder(shopID, orderID, status, startDate, endDate);
                    if (dto.error == null) 
                    {
                        this.dgvOrder.ItemsSource = dto.data;
                    }
                    else
                    {
                        MessageBox.Show(dto.error.msg);
                    }
                }
            }     
        }

        
    }
}
